package zyy.wcx.datastructures.queue;

import java.util.Scanner;

/**
 * @program: data-structures-algorithms
 * @description: 环形队列
 * @author: WuChengXing
 * @create: 2020-07-07 19:28
 **/
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("a(add): 往队列里面添加数据");
            System.out.println("g(get): 取出队列里面的数据");
            System.out.println("h(head): 取出队列头");
            // 接收一个字符
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    try {
                        arrayQueue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'a':
                    try {
                        System.out.println("请输入你需要添加的数据：");
                        int value = scanner.nextInt();
                        arrayQueue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        arrayQueue.getQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = arrayQueue.headQueue();
                        System.out.println(head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("成功退出程序==-==");
    }
}

class CircleArrayQueue{
    /**
     * 数组的最大长度
     */
    private int maxSize;

    /**
     * 队列头指针
     */
    private int front;

    /**
     * 队列尾指针
     */
    private int rear;

    /**
     * 普通数组
     */
    private int[] arr;

    /**
     * 功能描述: 初始化
     * @author 吴呈兴
     * @date 2020/7/7
     * @param maxSize
     * @return
     */
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }

    /**
     * 功能描述: 判断队列是否为空
     *
     * @param
     * @return boolean
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 功能描述: 判断队列是否为空
     *
     * @param
     * @return boolean
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 功能描述: 向队列中添加数据
     *
     * @param value
     * @return void
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public void addQueue(int value) {
        if (isFull()) {
            throw new RuntimeException("队列已经满了！！无法继续添加数据");
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 功能描述: 向队列里面取数据（从头部开始取）
     *
     * @param
     * @return int
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("当前队列是空的，么得数据！");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 功能描述: 打印队列
     *
     * @param
     * @return void
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public void showQueue() {
        if (isEmpty()) {
            throw new RuntimeException("当前队列是空的，么得数据！");
        }
        for (int i = front; i < front + getValidSize(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    public int getValidSize(){
        return (rear + maxSize - front) % maxSize;
    }

    /**
     * 功能描述: 获取队列的头
     *
     * @param
     * @return int
     * @author 吴呈兴
     * @date 2020/7/7
     */
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("当前队列是空的，么得数据！");
        }
        return arr[front];
    }
}